#ifndef XMLHTTPREQUESTCLASS_H
#define XMLHTTPREQUESTCLASS_H

#include "jsapi.h"
#include "js-engine.h"

enum state{
    UNSENT, OPENED, HEADERS_RECEIVED, LOADING, DONE
};

struct _xmlHttpRequest
{
    gushort readyState;

    gchar *method;
    gchar *url;
    gboolean async;
    gchar *user;
    gchar *password;
    gchar *query;
  
    GHashTable *requestHeaders;
    GHashTable *responseHeaders;

    GString *responseHeader;
    GString *responseText;
    gushort status;

    gushort result;

    GThread *thread;
    JSContext *cx;
    JSObject *obj;
};

typedef struct _xmlHttpRequest xmlHttpRequest;

gboolean XMLHttpRequestClass_define_constructor(js_engine *engine);

xmlHttpRequest *xmlHttpRequest_new(JSContext *cx, JSObject *obj);
void xmlHttpRequest_free(xmlHttpRequest *xhr);
gboolean xmlHttpRequest_open(xmlHttpRequest *xhr, const gchar *method, const gchar *url, gboolean async, const gchar *user, const gchar *password);
void xmlHttpRequest_setRequestHeader(xmlHttpRequest *xhr, const gchar *header, const gchar *value);
void xmlHttpRequest_setResponseHeader(xmlHttpRequest *xhr, const gchar *header, const gchar *value);
gchar *xmlHttpRequest_getAllResponseHeaders(xmlHttpRequest *xhr);
gchar *xmlHttpRequest_getResponseHeader(xmlHttpRequest *xhr, const gchar *header);
gchar *xmlHttpRequest_responseText(xmlHttpRequest *xhr);
unsigned short xmlHttpRequest_send(xmlHttpRequest *xhr, const gchar *query);
gboolean xmlHttpRequest_abort(xmlHttpRequest *xhr);

gboolean js_engine_define_constructors(js_engine *engine);
#endif
